package net.csdn.business.test;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import net.csdn.business.common.domain.request.UserRegMigrateDto;
import net.csdn.business.common.domain.vo.UserVo;
import net.csdn.business.facade.UserFacade;
import org.apache.commons.compress.utils.Lists;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.io.*;
import java.util.List;

@RestController
@Slf4j
public class fixUserMigrateData {

    @Autowired
    UserFacade userFacade;


    @GetMapping("/test")
    public List<String> test(String fileName){

        try {
            File f = new File(fileName);
            Reader reader = new InputStreamReader(new FileInputStream(f), "Utf-8");
            int ch = 0;
            StringBuffer sb = new StringBuffer();
            while ((ch = reader.read()) != -1) {
                sb.append((char) ch);
            }
            reader.close();
            String jsonStr = sb.toString();
            JSONArray objects = JSONArray.parseArray(jsonStr);
            List<String> strings = this.fixUserNoMigrateLog(objects);
            return strings;
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }





    @PostMapping("/fix")
    public List<String>  fixUserNoMigrateLog(@RequestBody JSONArray list){
            List<UserRegMigrateDto> UserList = Lists.newArrayList();
            list.forEach(i->{
                JSONObject ii = JSONObject.parseObject(JSONObject.toJSONString(i));
                UserRegMigrateDto userRegMigrateDto = new UserRegMigrateDto();
                userRegMigrateDto.setName(ii.getString("name"));
                userRegMigrateDto.setUsername(ii.getString("username"));
                userRegMigrateDto.setEmail(StringUtils.isNotEmpty(ii.getString("email"))? ii.getString("email") : ii.getString("username")+ "@atomgit.com");
                userRegMigrateDto.setExternalId(ii.getInteger("id").toString());
                userRegMigrateDto.setPhone(ii.getString("phone"));
                UserList.add(userRegMigrateDto);
            });
            List<String> result =Lists.newArrayList();
            for (UserRegMigrateDto u :UserList) {
                try {
                    String userId = userFacade.queryForMigrateUser(u);
                    if(StringUtils.isEmpty(userId)){

                        UserVo userVo = null;
                        try {
                            userVo = userFacade.queryUserByEmail(u.getEmail());
                        } catch (Exception e) {
                            //throw new RuntimeException(e);
                        }
                        if(ObjectUtils.isNotEmpty(userVo)){
                                String format = String.format("insert into migrate_user_baseinfo (user_name,email,origin_id,user_id,regist_result,origin_username,created_date,phone,channel) values('%s','%s','%s','%s',1,'%s',now(),'%s','gitee');",
                                        u.getUsername(), userVo.getEmail(), u.getExternalId(), userVo.getId(), u.getUsername(),StringUtils.isBlank(u.getPhone())?null:u.getPhone()
                                );
                                result.add(format);
                            }else{
                                String format = String.format("insert into migrate_user_baseinfo (user_name,email,origin_id,user_id,regist_result,origin_username,created_date,phone,channel) values('%s','%s','%s','%s',0,'%s',now(),'%s','gitee');",
                                        u.getUsername(), u.getEmail(), u.getExternalId(), null, u.getUsername(),StringUtils.isBlank(u.getPhone())?null:u.getPhone()
                                );
                                result.add(format);
                            }
                    }
                } catch (Exception e) {
                    log.error("fixUserNoMigrateLog",e);
                }
            }
                return result;
    }
}
